package com.example.imtoken.modules.main.wallet.details.trade.transfer.set_price

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ColorUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.ActivitySettingMinerFeeBinding
import com.example.imtoken.entitys.http.GasEntity
import com.xuexiang.xui.adapter.recyclerview.DividerItemDecoration

/**
 * 设置矿工费
 * @property adapter SettingMinerFeeAdapter
 */
class SettingMinerFeeActivity :
    BaseActivity<SettingMinerFeeViewModel, ActivitySettingMinerFeeBinding>(SettingMinerFeeViewModel::class.java) {

    private lateinit var adapter: SettingMinerFeeAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_setting_miner_fee
    }

    override fun init() {
        initBar(true, R.color.fragment_bg)
        BusUtils.register(this)
        initTitle()
        initClick()
        initAdapter()
        initObserve()
    }

    private fun initAdapter() {
        adapter = SettingMinerFeeAdapter(R.layout.item_setting_miner_fee)
        adapter.setDiffCallback(SettingMinerDiffCallBack())
        val layoutManager = LinearLayoutManager(this)
        dataBinding.rvSetting.layoutManager = layoutManager
        dataBinding.rvSetting.adapter = adapter
        dataBinding.rvSetting.addItemDecoration(
            DividerItemDecoration(
                this,
                LinearLayoutManager.VERTICAL
            )
        )
        adapter.setOnItemClickListener { _, _, position ->
            viewModel.updateSelected(position)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initObserve() {
        viewModel.getGasEntity().observe(this) {
            dataBinding.tvMinerFeeCalculate.text = getString(
                R.string.minerFeeCalculate,
                "${it.steps[it.defPosition].gas} ${it.uint}", it.gas.toString()
            )
            adapter.setUint(it.uint)
        }
        viewModel.getMinerFee().observe(this) {
            dataBinding.tvPrice.text = "$it ETH"
        }
        viewModel.getStepEntity().observe(this) {
            adapter.setDiffNewData(it)
        }
    }

    private fun initClick() {
        dataBinding.include10.ivHeadBack.setOnClickListener { this.finish() }
        dataBinding.tvConfirm.setOnClickListener {
            setResult(200, intent.putExtra("position", viewModel.oldP))
            this.finish()
        }
    }

    private fun initTitle() {
        dataBinding.include10.tvHeadTitles.text = getString(R.string.minerFeeSetting)
        dataBinding.include10.clHeadHome.setBackgroundColor(ColorUtils.getColor(R.color.fragment_bg))
        dataBinding.include10.vLine.visibility = View.INVISIBLE
    }

    @BusUtils.Bus(tag = BusConfig.out_data_sticky_select_setting_mainer_fee_activity, sticky = true)
    fun data(gasEntity: GasEntity) {
        viewModel.setGasEntityValue(gasEntity)
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.removeSticky(BusConfig.out_data_sticky_select_setting_mainer_fee_activity)
        BusUtils.unregister(this)
    }

}